<?php
/**
 * api.php
 * @author liuch
 * @version 1.0
 * @date 2017-07-28 14:25
 */
define('ROOT_PATH', realpath(__DIR__) . DIRECTORY_SEPARATOR);
//error_reporting(E_ALL);
//ini_set("display_errors", 1);

require_once ROOT_PATH . 'PreContract.php';
require_once ROOT_PATH . 'common.php';
require_once ROOT_PATH . 'config.php';
$preContract = new PreContract();

$preContractDays = $config['days'];
$cardNo = $config['card'];
$deptName = $config['dept'];
$doctorName = $config['doctor'];

if($registerDay = getRegisterDay($preContractDays)) {

    $start = time();

    $processTime = 0;

    $interval = 60;

    while (true) {

        $processStart = microtime (true);

        $res = $preContract->doPreContract($deptName, $doctorName, $registerDay, $user[$cardNo]);

        if($res['do']) {
            echo date('Y-m-d H:i:s') . " : 不错，抢到 {$res['doctor']['titleName']} {$res['doctor']['staffName']} " . implode(',', $res['order']) . PHP_EOL;
            $registerDay = getRegisterDay($preContractDays);
        } elseif($res['source']){
            echo date('Y-m-d H:i:s') . ' : 还有号源,继续抢' . PHP_EOL;
        } else {
            echo date('Y-m-d H:i:s') . ' : 没有号源了,不抢了' . PHP_EOL;
            break;
        }

        $processEnd = microtime (true);

        $processTime = max($processTime, $processEnd - $processStart);

        if ($processTime > $interval - (time() - $start)) {

            echo date('Y-m-d H:i:s') . ' : 实在抢不到,等下一波吧' . PHP_EOL;
            break;

        }

    }

} else {
    echo date('Y-m-d H:i:s') . ' : 没有预约' . PHP_EOL;
}
